
import { shrinkage, remoteMethod, setSonOptions, initSearch } from "@/utils/form";
import { reset } from "@/utils/table";
import { settingsQueryList } from "@/api/useDataGrid";
import { customerQueryLike, processPondQueryInProcess, processPondQueryList, modelQueryLike, settingsQueryDateTime } from "@/api/useDataGrid2";

import cloneDeep from "lodash/cloneDeep";

export default {
  data() {
    return {
      searchFormApi: {
        reportType: {
          api: settingsQueryList,
          params: {
            "type": "QxReport"
          },
          displayField: ["value", "value"],
          backfill: true,
          options: []
        },
        mainPondCode: {
          api: processPondQueryInProcess,
          data: {
            processCode: "SJ"
          },
          displayField: ["name", "id"],
          backfill: true,
          options: [],
          childern: [
            {
              sonLineCode: {
                api: processPondQueryList,
                params: {
                  "fatherId": ""
                },
                displayField: ["name", "code"],
                backfill: true,
                options: []
              }
            }
          ]
        }

      },
      // 查询条件
      searchForm: {
        initSearch: (db) => initSearch(db, true, "searchForm", this),
        // 刷新方法
        refresh: null,
        // 使用表单组件后所有的值
        form: null,
        options: [
          {
            type: "input",
            value: "QxReport",
            label: "",
            hide: true,
            prop: "reportModel"
          },
          {
            type: "custom-date-time",
            value: [],
            label: "",
            prop: "customDateTime",
            hide: false,
            backfill: true
          },
          {
            type: "select",
            value: "复杂表格导出",
            label: "",
            hide: false,
            prop: "reportType",
            attrs: {
              style: {
                width: "150px"
              },
              clearable: true,
              placeholder: "请选择报表类型"
            },
            childern: []
          },
          {
            type: "select",
            value: "",
            label: "",
            hide: false,
            prop: "classType",
            attrs: {
              style: {
                width: "150px"
              },
              clearable: true,
              placeholder: "请选择班别"
            },
            childern: [
              {
                lable: "白班",
                value: "白班",
                type: "option"
              },
              {
                lable: "夜班",
                value: "夜班",
                type: "option"

              }
            ]
          },
          {
            type: "select",
            value: "",
            label: "",
            hide: false,
            prop: "processCode",
            attrs: {
              style: {
                width: "150px"
              },
              clearable: true,
              placeholder: "线体"
            },
            childern: []
          },
          {
            type: "select",
            value: [],
            label: "",
            hide: false,
            prop: "customerCode",
            attrs: {
              style: {
                width: "150px"
              },
              loading: false,
              clearable: true,
              placeholder: "客户名称",
              filterable: true,
              remote: true,
              reserveKeyword: true,
              remoteMethod: (query) => remoteMethod("searchForm", "customerCode", this, customerQueryLike, query, ["name", "code"])
            },
            childern: []
          },
          {
            type: "select",
            value: "",
            label: "",
            hide: false,
            prop: "modelName",
            attrs: {
              style: {
                width: "150px"
              },
              clearable: true,
              loading: false,
              placeholder: "产品品名",
              filterable: true,
              remote: true,
              reserveKeyword: true,
              remoteMethod: (query) => remoteMethod("searchForm", "modelName", this, customerQueryLike, query, ["name", "name"])
            },
            childern: []
          },
          {
            type: "select",
            value: "",
            label: "",
            hide: false,
            prop: "modelModel",
            attrs: {
              style: {
                width: "150px"
              },
              loading: false,
              clearable: true,
              placeholder: "产品型号",
              filterable: true,
              remote: true,
              reserveKeyword: true,
              remoteMethod: (query) => remoteMethod("searchForm", "modelModel", this, modelQueryLike, query, ["name", "name"])
            },
            childern: []
          },
          {
            type: "select",
            value: "",
            label: "----------------------------测试多级联动",
            hide: false,
            prop: "mainPondCode",
            attrs: {
              style: {
                width: "150px"
              },
              clearable: true,
              placeholder: "主线体"
            },
            childern: [],
            changeFun: (item, index, form) => setSonOptions("searchForm", this, "sonLineCode", processPondQueryList, { fatherId: form.mainPondCode }, true, ["name", "code"])
          },
          {
            type: "select",
            value: "",
            label: "",
            hide: false,
            prop: "sonLineCode",
            attrs: {
              style: {
                width: "150px"
              },
              clearable: true,
              placeholder: "子线体"
            },
            childern: []
          }

        ],
        btnList: {
          show: true,
          size: "mini",
          btnList: [
            {
              label: "展开",
              mthods: () => shrinkage(2, 0, "searchForm", "isOpenSearch", this, "dataGrid", 210),
              icon: "menu-unfold"
            },
            {
              label: "查询",
              mthods: (form) => this.search(form),
              icon: "el-icon-search",
              type: "primary"
            },
            {
              label: "重置",
              mthods: (form, search) => reset(["codeNumber", "mainPondCode", "sonLineCode", "reportType"], form, search),
              icon: "el-icon-refresh"
            },
            {
              label: "复杂表格导出",
              mthods: (form, search) => this.download(),
              icon: "el-icon-download"
            }
          ]

        }
      },
      tableHead: {
        amount: "",
        goodAmount: "",
        ngTotalAmount: "",
        goodRate: "",
        ngRate: "",
        ngTypeList: []
      }

    };
  },
  created() {
    this.init();
  },
  computed: {
    ngList() {
      return this.tableHead.ngTypeList.filter((item) => item.type === "制程不良") || [];
    },
    userNgList() {
      return this.tableHead.ngTypeList.filter((item) => item.type === "素材不良") || [];
    }
  },
  methods: {
    //
    async init() {
      // 查询表单中所有的需要初始化是需要调用的接口
      // await setInitOptions("searchForm", this, this.searchFormApi);
      // await this.defaultDate();
      // await this.searchForm.refresh();
    },
    async defaultDate() {
      const { staDateTime, endDateTime } = await settingsQueryDateTime();
      this.searchForm.form.customDateTime = [staDateTime, endDateTime];
    },
    // 查询
    async search(form) {
      this.setData();
      console.log("使用");
      // // 将分页数据插入查询表单
      // let _form = getPagination(form, "dataGrid", this);
      // // 时间字段装换
      // _form = transitionDateFormat(_form, "customDateTime");

      // getTableData("dataGrid", this, reportQueryListReportInspectForm, _form, true,
      //   async(data) => {

      //   }
      // );
    },
    addColumns(field) {
      for (let i = 0; i < this[field].length; i++) {
        const { name, id } = this[field][i] || {};
        this.dataGrid.columns.push({
          label: name,
          prop: id + "-",
          align: "center"
        });
      }
    },
    async setData() {
      // 直接使用的本地数据
      const { header, formList } = this.exportTestData || {};
      console.log(header, "headerheaderheader");

      const { ngTypeList, amount, goodAmount, ngTotalAmount, goodRate, ngRate } = header || {};

      this.tableHead = Object.assign(this.tableHead, {
        amount: amount + "",
        goodAmount: goodAmount + "",
        ngTotalAmount: ngTotalAmount + "",
        goodRate: goodRate + "",
        ngRate: ngRate + "",
        ngTypeList
      });

      this.dataGrid.data = formList.map((item) => {
        const { incomingType, incomingModel, modelName, customerName, date, totalAmount, goodAmount, ngTotalAmount, goodRate, ngRate, customer, model, ngTypeList } = item;

        const obj = {};

        for (let i = 0; i < ngTypeList.length; i++) {
          const { id, amountObj } = ngTypeList[i];
          obj[id + "-"] = amountObj + "";
        }

        return {
          ...obj, incomingType, incomingModel, modelName,
          date, totalAmount, goodAmount, ngTotalAmount: ngTotalAmount || "0", goodRate, ngRate, customer, Model: model, name: modelName, userName: customerName
        };
      });
      this.dataGrid.pagination.total = formList.length;

      this.dataGrid.columns = cloneDeep(this.dataGrid.oldColums);

      await this.addColumns("ngList");
      await this.addColumns("userNgList");
    }
  }
};
